import {
  bannerList,
  functionList,
  personalFunctionList
} from '../../utils/http'
Page({
  data: {
    pageIndex: 0,
    bannerList,
    functionList,
    personalFunctionList,
    showContainer: false,
  },
  async onLoad() {
    let userinfo = wx.getStorageSync('userinfo')
    if (userinfo) {
      this.setData({
        userinfo
      })
    } else {
      this.setData({
        userinfo: {
          avatarUrl: "https://img1.baidu.com/it/u=3598104138,3632108415&fm=253&fmt=auto&app=120&f=JPEG?w=800&h=800",
          nickName: "微信用户",
          id: 28193,
          points: 0,
          signin: 0,
          message: 0
        }
      })
      wx.setStorageSync('userinfo', this.data.userinfo)
    }
    let app = getApp()
    this.setData({
      topTotalHeight: app.globalData.topTotalHeight
    })
    // // 获取轮播图
    // let res2 = await bannerList()
    // if (res2.code !== 200) {
    //   wx.showToast({
    //     title: res.data,
    //     icon: 'error'
    //   })
    //   return
    // }
    this.setData({
      bannerList: bannerList
    })
    // 获取功能列表
    // let res = await functionList()
    // if (res.code !== 200) {
    //   wx.showToast({
    //     title: res.data,
    //     icon: 'error'
    //   })
    //   return
    // }
    // 筛选可以展示的功能列表
    let homeFunctionList = [],
      allFunctonList = []
    functionList.map(item => {
      let homeList = {
        name: item.name,
        list: []
      }
      let allList = {
        name: item.name,
        list: []
      }
      allList.list = item.list.filter(item2 => item2.enabled)
      if (allList.list.length > 0) allFunctonList.push(allList)
      homeList.list = allList.list.filter(item2 => item2.showFirstPage)
      if (homeList.list.length > 0) homeFunctionList.push(homeList)
    })
    this.setData({
      homeFunctionList,
      allFunctonList
    })
    // 获取个人主页functionlist
    // let res3 = await personalFunctionList()
    // if (res3.code !== 200) {
    //   wx.showToast({
    //     title: res.data,
    //     icon: 'error'
    //   })
    //   return
    // }
    this.setData({
      personalFunctionList: personalFunctionList
    })
  },
  /**
   * 页面跳转
   * @param {*} e 
   */
  toPage(e) {
    let {
      value
    } = e.currentTarget.dataset
    wx.navigateTo({
      url: value,
    })
  },
  /**
   * 页面切换
   * @param {*} e 
   */
  switchtab(e) {
    this.setData({
      pageIndex: e.detail
    })
  },
  /**
   * 页面分享
   */
  onShareAppMessage() {
    return {
      title: '工具集',
      path: '/pages/home/index',

    }
  },
  /**
   * 展示半屏页
   */
  showWeixinCode() {
    this.setData({
      showContainer: true
    })
  },
  /**
   * 开发中
   */
  developing() {
    wx.showToast({
      title: '开发中...',
      icon: 'error'
    })
  },
  // 更换用户头像
  async chooseAvatarImage(e) {
    if (e.detail.avatarUrl) {
      let filePath = e.detail.avatarUrl
      this.setData({
        'userinfo.avatarUrl': filePath
      })
      wx.setStorageSync('userinfo', this.data.userinfo)
      // let res = await fileUpload(filePath)
      // if (res.statusCode !== 20000) {
      //   return wx.showToast({
      //     title: res.statusMsg,
      //     icon: 'none'
      //   })
      // }
      // console.log(res.files[0])
      // let res2 = await userinfoEdit({
      //   avatarUrl: res.data,
      //   nickname: this.data.userinfo.nickName
      // })
      // if (res2.statusCode !== 20000) {
      //   return wx.showToast({
      //     title: res2.statusMsg,
      //     icon: 'none'
      //   })
      // }
      // this.getUserInfo()
    }
  },
  // 昵称输入
  nicknameInput(e) {
    this.nickname = e.detail.value
  },
  // 更换昵称
  async nicknamereview(e) {
    console.log(e)
    if (e.detail.pass && this.nickname) {
      this.setData({
        'userinfo.nickName': this.nickname
      })
      wx.setStorageSync('userinfo', this.data.userinfo)
      // let res2 = await userinfoEdit({
      //   nickname: this.data.nickname
      // })
      // if (res2.statusCode !== 20000) {
      //   return wx.showToast({
      //     title: res2.statusMsg,
      //     icon: 'none'
      //   })
      // }
      // this.getUserInfo()
    } else {
      this.setData({
        'userinfo.nickName': this.data.userinfo.nickName
      })
    }
  },
  lookMore() {
    this.setData({
      pageIndex: 1
    })
  },
  onShareAppMessage() {
    return {
      title: "多功能小程序，应有尽有",
      path: "/pages/home/index",
      imageUrl: '/images/function_icon/kunde.png',
    }
  },
  onShareTimeline() {
    return {
      title: '多功能小程序，应有尽有',
      imageUrl: '/images/function_icon/kunde.png',
    }
  },
})